<?php
class LifeAction extends CommonAction {

	protected $currentModel=null;
	protected $cuurentAlbum=null;
	protected $childCategory='';
	protected $cacheData=array();
	protected $currentCategoryId=7;

	public function _initialize()
	{
		parent::_initialize();
		$this->currentModel = D('Category');
		$this->cuurentAlbum = D('Album');
		$this->cacheCategoryData=get_cache('Category');
	}	
	
	public function index()
	{
		$id=$this->_get($this->currentModel->getPk());
		if(!isset($id))
		{
			$id=$this->currentCategoryId;
		}
		$categoryWhere['id']=$id;
		$categoryWhere['model']='Album';		
		$categoryData=$this->currentModel->where($categoryWhere)->find();
	  	if(empty($categoryData))
	  	{
		  $this->error("数据不存在！",'/');
	  	}
    	$seo=seo($categoryData);
    	$categoryChild=$this->getChildCategory($categoryData['id']);
    	if(empty($categoryChild))
    	{
    		$categoryChild=$this->getChildCategory($this->currentCategoryId);
    		$this->currentCategoryId=$categoryData['id'];
    	}else
    		{
    			$this->currentCategoryId=substr($this->childCategory,1);
    		}		
 		import ('@.ORG.Page');
 		$map['category_id']=array('in',$this->currentCategoryId);
    	$map['stauts']=1;
    	$dataModel=D(ucfirst($categoryData['model']));
    	$count=$dataModel->where($map)->count();
    	$page = new Page($count,C ('PAGE_LIMIT'));
    	$datas=$dataModel->where($map)->relation('Comment')->limit($page->firstRow.','.$page->listRows)->order('id DESC')->select();
    	$this->assign('categoryChild',$categoryChild);
    	$this->assign('page',$page->show());
    	$this->assign('datas',$datas);
    	$this->assign('seo',$seo);
    	$this->assign('categoryData',$categoryData);    	    		
		$this->display();   	
	}

	public function view()
	{
		$id=$this->_get($this->cuurentAlbum->getPk());
		$data=$this->cuurentAlbum->find($id);
	  	if(empty($data))
	  	{
		  $this->error("数据不存在！",U('Index/index'));
	  	}
	  	$albumGallery=M('AlbumGallery');
	  	$allAlbum=$albumGallery->where(array('product_id'=>$data['id']))->order('sort_order ASC')->select();
    	$seo=seo($data);
    	$comment=M('Comment')->where(array('value_id'=>$id,'status'=>1,'source'=>'Album'))->order('id DESC')->select();
    	$comment=$this->childComment($comment);
    	$categoryChild=$this->getChildCategory($this->currentCategoryId);
    	$this->upHit('Album',$data['id']);
    	$this->assign('categoryChild',$categoryChild);
    	$this->assign('seo',$seo);
    	$this->assign('data',$data);
    	$this->assign('comment',$comment);
    	$this->assign('categoryInfo',$this->cacheCategoryData[$data['category_id']]);
    	$this->assign('prevInfo',$this->getPrevInfo('Album',$data['id']));
    	$this->assign('nextInfo',$this->getNextInfo('Album',$data['id']));
    	$this->assign('allAlbum',$allAlbum);
    	$this->display();
	}	

	private function getChildCategory($id)
	{
		foreach($this->cacheCategoryData as $d)
		{
			if($d['parent_id']==$id)
			{
				$child[]=$d;
				$this->childCategory.=','.$d['id'];
			}
		}
		return $child;
	}
}